// index.js

Page({
  data: {
    classifyData:[
      {
        icon:'icon-gongzuotai',
        text:'工作'
      },
      {
        icon:'icon-xuexi',
        text:'学习'
      },
      {
        icon:'icon-sikao',
        text:'思考'
      },
      {
        icon:'icon-woxiezuoye',
        text:'写作'
      },
      {
        icon:'icon-yundong',
        text:'运动'
      },
      {
        icon:'icon-yuedu',
        text:'阅读'
      },
    ],
    current_index:0,
    slider_val:1,
    //当前的分钟
    slider_cur:60000,
    //储存当期的分钟
    current_mm:0,
    //显示变量
    is_show:false,
    //动态时间
    detail_time:'00:00',
    //设备比例
    ratio:1,
    //暂停
    suspend:false,
    //返回
    back:true,
    //继续
    continue:true,
    //退出
    quit:true,
    //定时器
    times:null
  },
  //获取当前点击的
  click_color(e){
    this.setData({
      current_index:e.currentTarget.dataset.index
    })
  },

  //获取滑动的值
  slider(e){
    let detail_time = this.formatdate(e.detail.value * 60 * 1000)


    this.setData({
      slider_val:e.detail.value,
      slider_cur:this.data.slider_val * 60 * 1000,
      detail_time
    })
  },

  //点击暂停
  suspendBtn(){
    this.setData({
      suspend:true,
      //继续
      continue:false,
      //退出
      quit:false
    })
    clearInterval(this.data.times)
  },

  //点击继续
  continueBtn(){
    this.setData({
      suspend:false,
      //继续
      continue:true,
      //退出
      quit:true
    })
    this.canvas2()
  },

  //点击退出
  quitBtn(){
    this.setData({
      suspend:false,
      //继续
      continue:true,
      //退出
      quit:true,
      is_show:false
    })
    clearInterval(this.data.times)
  },

  //点击返回
  backBtn(){
    this.setData({
      //暂停
      suspend:false,
      //返回
      back:true,
      //继续
      continue:true,
      //退出
      quit:true,
      is_show:false
    })
    clearInterval(this.data.times)
  },

  //格式化时间
  formatdate(timess){
    let times = new Date(timess)
    let hh = (times.getHours() - 8) < 10 ? '0' + (times.getHours() - 8) : times.getHours() - 8
    let mm = times.getMinutes() < 10 ? '0' + times.getMinutes() : times.getMinutes()
    let ss = times.getSeconds() < 10 ? '0' + times.getSeconds() : times.getSeconds()

    return `${hh}:${mm}:${ss}`
  },

  //格式年月日
  formatYMD(){
    let Datas = new Date()
    let YY = Datas.getFullYear()
    let MM = Datas.getMonth() + 1
    let DD = Datas.getDate()
    let hh = Datas.getHours()
    let mm = Datas.getMinutes()
    let ss = Datas.getSeconds()
    return `${YY}-${MM}-${DD} ${hh}:${mm}:${ss}`
  },

  //点击是否显示页面
  is_show(){
    this.setData({
      is_show: !this.data.is_show,
      slider_cur:this.data.slider_val * 60 * 1000,
    })
    this.canvas()
    this.canvas2()
  },

  //画圆
  canvas(){
    //转换成px
    const linewight = 6 / this.data.ratio;
    const ctx = wx.createCanvasContext('canvas1')
    ctx.setLineWidth(linewight)
    ctx.setStrokeStyle("#FFFFFF")
    ctx.setLineCap("round")
    ctx.beginPath()
    ctx.arc(400/this.data.ratio/2,400/this.data.ratio/2,400/this.data.ratio/2 - 2 * linewight,0,2*Math.PI,true)
    ctx.stroke()
    ctx.draw()
  },
  canvas2(){
    //转换成px
    const linewight = 6 / this.data.ratio;
    const ctx = wx.createCanvasContext('canvas2')
    ctx.setLineWidth(linewight)
    ctx.setStrokeStyle('#000000')
    ctx.setLineCap("round")
    
    let timer = 1.5
    
      this.data.times = setInterval(() => {
        if(timer < 3.5){
        timer = 1.5 + 2 * (this.data.slider_val * 60 * 1000 - this.data.slider_cur) / (this.data.slider_val * 60 * 1000)
        this.setData({
          slider_cur: this.data.slider_cur - 100,
          detail_time: this.formatdate(this.data.slider_cur),
          current_mm: this.data.current_mm + 100
        })
        ctx.beginPath()
        ctx.arc(400/this.data.ratio/2,400/this.data.ratio/2,400/this.data.ratio/2 - 2 * linewight,
          1.5 * Math.PI,timer * Math.PI,true)
        ctx.stroke()
        ctx.draw()
        }else{
          let taskDetail = wx.getStorageSync('taskdetail') || []
          taskDetail.unshift({
            YMD:this.formatYMD(),
            work: this.data.classifyData[this.data.current_index].text,
            mm: new Date(this.data.current_mm).getMinutes()
          })
          wx.setStorageSync('taskdetail', taskDetail)

          this.setData({
            //暂停
            suspend:true,
            //返回
            back:false,
            //继续
            continue:true,
            //退出
            quit:true,
          })
          clearInterval(this.data.times)
        }
      },100)
   
    
    
  },
  onLoad(){
    //拿到每个设配的比例
    wx.getSystemInfo({
      success: (result) => {
        this.setData({
          //计算出设备的比例
          ratio:750 / result.windowWidth
        })
      },
    })
  }
})
